<?php

namespace app\result;

use app\model\AppVersion;
use think\helper\Arr;

class PackageResult extends BaseResult
{
    public function __construct()
    {
        $this->model = new AppVersion();
        parent::__construct();
    }

    public function list(array $params)
    {
        $page = request()->param('page', 1, 'intval');
        $pageSize = request()->param('limit', 10, 'intval');
        $platform = request()->param('platform', 0, 'trim');
        $where = [];
        if (!empty($params['keyword'])) {
            $where[] = ['tips', 'like', '%' . trim($params['keyword']) . '%'];
        }
        if ($platform) {
            $where[] = ['platform', '=', $platform];
        }
        $total = $this->model->data_count($where);
        $data = $this->model->data_list($where, '*', 'updated_at desc', $page, $pageSize);
        return $this->success_return('success', [
            'total' => $total,
            'list' => $data
        ]);
    }

    public function get($id)
    {
        return $this->success_return("success", $this->model->data_get(['id' => $id]));
    }

    public function add($param)
    {
        if (empty($param['url'])) {
            return $this->error_return('请先上传安装包');
        }
        if ($param['platform'] == 'ios' || $param['platform'] == 'android') {
            if (empty($param['version'])) {
                return $this->error_return('请输入版本号');
            }
            if (!empty($param['wgt_flag']) && empty($param['wgt_version'])) {
                return $this->error_return('请输入热资源包版本号');
            }
        } else {
            $param['is_test'] = 0;
        }
        $model = new AppVersion();
        if (in_array($param['platform'], ['manager', 'h5']) && !is_numeric($param['version'])) {
            $version = $model->get_value(['platform' => $param['platform']], 'version', 'version desc');
            $param['version'] = $version ? $version + 1 : 1000;
        }

        $res = $model->save($param);
        if (!$res) {
            return $this->error_return('提交保存失败');
        }
        return $this->success_return();
    }

    public function update($id, $param)
    {
        $data = AppVersion::find($id);
        if (!$data) {
            return $this->error_return("未找到该记录");
        }
        if (in_array($param['platform'], ['manager', 'h5']) && !is_numeric($param['version'])) {
            return $this->error_return('测试版版本号必须为数字');
        }
        if (!empty($param['wgt_flag']) && empty($param['wgt_version'])) {
            return $this->error_return('请输入热资源包版本号');
        }
        $data->save(Arr::only($param, [
            'platform',
            'version',
            'wgt_version',
            'tips',
            'url',
            'status',
            'is_force',
            'is_test',
            'wgt_flag'
        ]));


        return $this->success_return();
    }

    public function delete($id)
    {
        AppVersion::destroy($id);
        return $this->success_return();
    }

    public function state($id)
    {
        $data = AppVersion::find($id);
        if (!$data) {
            return $this->error_return("未找到该记录");
        }
        $data->status = (int)!$data->status;
        $data->save();
        return $this->success_return();
    }

    //开始更新
    public function start_update($param)
    {
        $id = $param['id'] ?? 0;
        if (!$id) {
            return $this->error_return('参数错误');
        }
        $data = $this->model->data_get(['id' => $id]);
        if (!$data) {
            return $this->error_return('未找到该记录');
        }
        //
        $platform = $data['platform'];//'android','ios','h5','wx_xcx','manager'
        if ($data['status'] !== 1) {
            return $this->error_return('当前版本未启用');
        }
        $secret = config('domain.update_secret');
        $token = md5($secret);
        $api = config('domain.domains.' . $platform);
        if (empty($secret) || empty($token)) {
            return $this->error_return('请先配置更新接口');
        }
        if (empty($api)) {
            return $this->error_return('暂不支持' . $platform);
        }
        //版本号自动+1，覆盖旧版本
        $version = (new AppVersion())->get_value(['platform' => $platform], 'version', 'version desc');
        (new AppVersion())->data_update(['id' => $id], ['version' => $version + 1]);
        $res = bs_curl_get($api, ['token' => $token], 50);
        return $this->success_return('更新请求结果：' . ($res ?? '更新失败'), $res);
    }
}
